// import React, {useState, useEffect} from 'react'
import {
    centerContentGET,
    centerContentPut,
    centerDirContent,
    centerDirDelete,
    // fileDownload,
    fileDownloadMiddle,
    fileTryUpload,
    fileUploadMiddle
} from './contentIndex';

//发送GET请求获取用户文件目录
export const asyncGetContent = async (props)=>{
    let loading = true;
    let fileItemInfo = null;
    if(!props.cookie || !props.directory) {
        return Promise.reject('cookie或目录缺失');
    }
    const response = await centerContentGET(props);
    if(response.status === 200){
        fileItemInfo = (response.data);
        loading = false;
        return [fileItemInfo,loading];
    }
    return Promise.reject('获取出错');
}

//发送PUT请求修改用户文件名称
export const asyncContentPut = async(props) =>{
    let Message = {
        succeed: false,
        msg:''
    };
    if(!props.cookie || !props.directory) {
        Message.succeed= false;
        Message.msg="cookie 或 路径缺失";
        return Promise.reject(Message.msg);
    }
    const response = await centerContentPut(props);
    Message.msg=response.data;
    if(response.status === 200){
        Message.succeed = true;
        return [Message];
    } else if (response.status===500){
        Message.succeed = false;
        return Promise.reject(Message.msg.message);
    }
}

//发送DELETE请求删除用户文件
export const asyncContentDelete = async(props) =>{
    let loading = true;
    let Message = {
        succeed: false,
        msg:''
    };
    if(!props.cookie || !props.directory) {
        loading = false;
        Message.succeed= false;
        Message.msg="cookie 或 路径缺失";
        return [Message, loading];
    }
    const response = await centerDirDelete(props);
    if(response.status === 200){
        Message.succeed = true;
        loading = false;
        return [Message,loading];
    } else if (response.status===500){
        Message.succeed = false;
        Message.msg='删除失败';
        loading = false;
        return [Message,loading];
    }
}

//发送POST请求
export const asyncContentPost = async(props) =>{
    let loading = true;
    let Message = {
        succeed: false,
        msg:''
    };
    if(!props.cookie || !props.directory) {
        loading = false;
        Message.succeed= false;
        Message.msg="cookie 或 路径缺失";
        return [Message, loading]
    }
    const response = await centerDirContent(props);
    if(response.status === 200){
        Message.succeed = true;
        loading = false;
        return [Message,loading];
    } else if (response.status===500){
        Message.succeed = false;
        Message.msg='新建失败';
        loading = false;
        return [Message,loading];
    }
}

//获取上传口令
export const asyncUploadToken = async(props) =>{
    let Message = {
        succeed: false,
        msg:''
    };
    if(!props.cookie || !props.file_name) {
        Message.succeed= false;
        Message.msg="token获取失败, cookie 或 文件名缺失";
        return Promise.reject(Message.msg);
    }
    const response = await fileUploadMiddle(props);
    if(response.status === 200){
        Message.succeed = true;
        Message.msg=response.data;
        return [Message];
    } else if (response.status===500){
        Message.succeed = false;
        Message.msg='token获取失败';
        return Promise.reject(Message.msg);
    }
}

//校验hash码
export const asyncTryUpload = async(props) =>{
    let Message = {
        succeed: false,
        msg:''
    };
    if(!props.auth || !props.hash) {
        Message.succeed= false;
        Message.msg="hash校验失败, token 或 hash缺失";
        return Promise.reject(Message.msg);
    }
    const response = await fileTryUpload(props);
    Message.msg=response.data;
    if(response.status === 200){
        Message.succeed = true;
        return [Message];
    } else if (response.status===500){
        Message.succeed = false;
        return Promise.reject(Message.msg.message);
    }
}

//获取下载口令
export const asyncDownloadToken = async(props) =>{
    let Message = {
        succeed: false,
        msg:''
    };
    if(!props.cookie || !props.file_id) {
        Message.succeed= false;
        Message.msg="cookie 或 文件名缺失";
        return Promise.reject(Message.msg);
    }
    const response = await fileDownloadMiddle(props);
    Message.msg=response.data;
    if(response.status === 200){
        Message.succeed = true;
        return [Message];
    } else if (response.status===500){
        Message.succeed = false;
        return Promise.reject(Message.msg.message);
    }
}

//下载请求
// export const asyncDownLoad = async(props) =>{
//     let Message = {
//         succeed: false,
//         msg:''
//     };
//     if(props.auth===undefined) {
//         Message.succeed= false;
//         Message.msg="auth 缺失";
//         return Promise.reject(Message.msg);
//     }
//     const response = await fileDownload(props);
//     Message.msg=response.data;
//     if(response.status === 200){
//         Message.succeed = true;
//         return [Message];
//     } else if (response.status===500){
//         Message.succeed = false;
//         return Promise.reject(Message.msg.message);
//     }
// }